package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.*;
import com.hushao.lims.weblims.repository.ClassRepository;
import com.hushao.lims.weblims.repository.TermRepository;
import com.hushao.lims.weblims.repository.TimetableRepository;
import com.hushao.lims.weblims.service.ClassService;
import com.hushao.lims.weblims.util.DateUtil;
import com.hushao.lims.weblims.util.TimetablesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 930588706
 * @date 18/2/1
 */
@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassRepository classRepository;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private TimetableRepository timetableRepository;

    @Autowired
    private TermRepository termRepository;
    @Autowired
    private TimetablesUtil timetablesUtil;
    @Override
    public Page<ClassInfo> listClassByName(String className, Pageable pageable) {
        className="%"+className+"%";
        return classRepository.findByClassNameLike(className,pageable);
    }

    @Override
    public ClassInfo getOne(Long classId) {
        return classRepository.getOne(classId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeClass(Long classId) throws Exception {
        classRepository.deleteById(classId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String saveOrUpdate(ClassInfo classInfo, String teacherNumber) throws Exception {
        TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(teacherNumber);
        String message;
        if(classInfo.getClassId()==null){
            classInfo.setTeacherInfo(teacher);
            message="添加班级成功！";
            classRepository.save(classInfo);
        }else {
            ClassInfo classNew=this.getOne(classInfo.getClassId());
            classNew.setTeacherInfo(teacher);
            classNew.setClassName(classInfo.getClassName());
            classNew.setClassSize(classInfo.getClassSize());
            message="修改班级信息成功！";
            classRepository.save(classNew);
        }
        return message;
    }

    @Override
    public Map<String, Map<String, String>> timetables(ClassInfo classInfo) throws Exception {
        //根据当前时间判断学期
        String term= DateUtil.getTermByThisDate();
        //根据判断出的学期获取此学期在表中的记录
        TermInfo termInfo=termRepository.findByTerm(term);
        //根据班级，学期，获取课程记录
        List<TimetableInfo> timetables=timetableRepository.findByClassInfoAndTermInfo(classInfo,termInfo);
        //根据 周次排序
        timetables=timetables.stream().sorted(Comparator.comparing(TimetableInfo::getCycle)).collect(Collectors.toList());
        //分组：1.根据课次(2.根据星期(3.根据试验室))  java8
        Map<String, Map<String, Map<ClassroomInfo, List<TimetableInfo>>>> map2=timetables.stream().collect(Collectors.groupingBy(TimetableInfo::getLessons,Collectors.groupingBy(TimetableInfo::getWeek,Collectors.groupingBy(TimetableInfo::getClassroomInfo))));
        //根据课次排序 java8
        Map<String, Map<String, Map<ClassroomInfo, List<TimetableInfo>>>> sortMap = new LinkedHashMap<>();
        map2.entrySet().stream().sorted(Map.Entry.<String, Map<String, Map<ClassroomInfo, List<TimetableInfo>>>>comparingByKey()).forEachOrdered(x -> sortMap.put(x.getKey(), x.getValue()));
        return timetablesUtil.getTimetablesByClassroom(sortMap);
    }



}
